home *** CD-ROM | disk | FTP | other *** search
- /*** The Pad version 1.0
- * Notepad desk accessory
- * by BILL FOSTER 6/03/87
- *
- * This program has been placed in the public domain and may
- * be distibuted freely. Any improvements to this code, or
- * donations please send info to
- *
- * attn: Bill Foster
- * Infolab Inc.
- * 23240 Lori Way
- * Hayward, CA 94541
- * ph (415) 581-0739
- *
- * Modify to run on COLOR systems
- */
- /*********************************************************************/
- /* Portions of this code Copyright C1985 by Atari Corp. */
- /* INCLUDE FILES */
- /*********************************************************************/
-
- #include <stdio.h.>
- #include <obdefs.h>
- #include <gemdefs.h>
- #include <osbind.h>
- #include <ctype.h>
-
- /*********************************************************************/
- /* DEFINES */
- /*********************************************************************/
-
- #define TRUE 1
- #define FALSE 0
- /* #define WI_KIND (SIZER|MOVER|FULLER|CLOSER|NAME) */
- #define WI_KIND (MOVER|CLOSER|NAME)
-
- #define NO_WINDOW (-1)
-
- #define MIN_WIDTH (2*gl_wbox)
- #define MIN_HEIGHT (3*gl_hbox)
-
- /*********************************************************************/
- /* EXTERNALS */
- /*********************************************************************/
-
- extern int gl_apid;
-
- /*********************************************************************/
- /* GLOBAL VARIABLES */
- /*********************************************************************/
-
- int gl_hchar;
- int gl_wchar;
- int gl_wbox;
- int gl_hbox; /* system sizes */
-
- int menu_id ; /* our menu id */
-
- int phys_handle; /* physical workstation handle */
- int handle; /* virtual workstation handle */
- int wi_handle; /* window handle */
- int top_window; /* handle of topped window */
-
- int xdesk,ydesk,hdesk,wdesk;
- int xold,yold,hold,wold;
- int xwork,ywork,hwork,wwork; /* desktop and work areas */
-
- int msgbuff[8]; /* event message buffer */
- int keycode; /* keycode returned by event-keyboard */
- int mx,my; /* mouse x and y pos. */
- int but; /* for event_multi */
- int ret; /* dummy return variable */
-
- int hidden; /* current state of cursor */
-
- int contrl[12];
- int intin[128];
- int ptsin[128];
- int intout[128];
- int ptsout[128]; /* storage wasted for idiotic bindings */
-
- int work_in[11]; /* Input to GSX parameter array */
- int work_out[57]; /* Output from GSX parameter array */
- int pxyarray[10]; /* input point array */
-
- /****************************************************************/
- /* GSX UTILITY ROUTINES. */
- /****************************************************************/
-
- hide_mouse()
- {
- if(! hidden){
- graf_mouse(M_OFF,0x0L);
- hidden=TRUE;
- }
- }
-
- show_mouse()
- {
- if(hidden){
- graf_mouse(M_ON,0x0L);
- hidden=FALSE;
- }
- }
-
- /****************************************************************/
- /* open virtual workstation */
- /****************************************************************/
- open_vwork()
- {
- register int i;
-
- for (i=0; i<10; work_in[i++] = 1);
- work_in[10] = 2;
- handle = phys_handle;
- v_opnvwk (work_in, &handle, work_out);
- }
-
- /****************************************************************/
- /* set clipping rectangle */
- /****************************************************************/
- set_clip (x, y, w, h)
- register int x, y;
- int w, h;
- {
- int clip[4];
- *clip = x;
- clip[1] = y;
- clip[2] = x+w;
- clip[3] = y+h;
- vs_clip (handle, 1, clip);
- }
-
- /****************************************************************/
- /* open window */
- /****************************************************************/
- open_window()
- {
- wi_handle = wind_create (WI_KIND, xdesk, ydesk, wdesk, hdesk);
- graf_growbox (xdesk+wdesk/2,ydesk+hdesk/2,gl_wbox,gl_hbox,xdesk,ydesk,wdesk,hdesk);
- wind_open (wi_handle, xdesk, ydesk, wdesk, hdesk);
- wind_get (wi_handle, WF_WORKXYWH, &xwork, &ywork, &wwork, &hwork);
- }
-
- /****************************************************************/
- /* find and redraw all clipping rectangles */
- /****************************************************************/
-
- do_redraw (xc, yc, wc, hc)
- int xc, yc, wc, hc;
- {
- GRECT t1, t2;
-
- hide_mouse ();
- wind_update (TRUE);
- t2.g_x = xc;
- t2.g_y = yc;
- t2.g_w = wc;
- t2.g_h = hc;
- wind_get (wi_handle, WF_FIRSTXYWH, &t1.g_x, &t1.g_y, &t1.g_w, &t1.g_h);
- while (t1.g_w && t1.g_h) {
- if (rc_intersect (&t2, &t1)) {
- set_clip (t1.g_x, t1.g_y, t1.g_w, t1.g_h);
- draw_sample ();
- }
- wind_get (wi_handle, WF_NEXTXYWH, &t1.g_x, &t1.g_y, &t1.g_w, &t1.g_h);
- }
- wind_update(FALSE);
- show_mouse();
- }
-
- /****************************************************************/
- /* Accessory Unit - Until First Event_Multi */
- /****************************************************************/
-
- #define PAD_FILE "notepad.sys"
- #define PAGES 9
- #define PAGE_SIZE (40*11)
- #define COLUMNS 40 /* Character width & height */
- #define LINES 11
-
- int ccol, cline; /* cursor column and row */
- int scol[PAGES], sline[PAGES]; /* Save cursor position */
- int page; /* Current page number */
- int cell_width;
- int cell_height; /* Height of character in pixels */
- char *pad_ram;
- char file[20];
- char pad[100];
-
- main ()
- {
- int attrib[10];
- register int fd, c;
- register long l;
- register char *p;
- extern char *calloc ();
-
- if ((pad_ram = calloc (PAGES, PAGE_SIZE)) != NULL)
- {
- appl_init ();
- phys_handle = graf_handle (&gl_wchar, &gl_hchar, &gl_wbox, &gl_hbox);
- menu_id = menu_register (gl_apid, " Notepad");
- wind_get (0, WF_WORKXYWH, &xdesk, &ydesk, &wdesk, &hdesk);
- c = Getrez ();
- if (c == 2)
- cell_height = 16;
- else
- cell_height = 8;
- cell_width = 8;
- hdesk /= 2; /* Half screen */
- wdesk /= 2; /* Quarter Screen */
- hdesk += cell_height / 2;
- xdesk += wdesk/2;
- wdesk += 2;
- ydesk += 25;
- wi_handle = NO_WINDOW;
- hidden = FALSE;
- page = ccol = cline = 0;
- p = pad_ram;
- for (c=0; c < (PAGE_SIZE * PAGES); c++) /* Clear workspace */
- *p++ = ' ';
- *file = 'A' + Dgetdrv ();
- file[1] = ':';
- strcpy (file+2, PAD_FILE);
- fd = Fopen (file, 0);
- if (fd >= 0)
- {
- l = (long)(PAGE_SIZE * PAGES);
- Fread (fd, l, pad_ram);
- Fclose (fd);
- }
- setbuf (stdout, NULL);
- multi ();
- }
- }
-
-
- /****************************************************************/
- /* dispatches all accessory tasks */
- /****************************************************************/
- multi()
- {
- int event;
-
- while (TRUE) {
- event = evnt_multi (MU_MESAG | MU_BUTTON | MU_KEYBD,
- 1,1,1,
- 0,0,0,0,0,
- 0,0,0,0,0,
- msgbuff, 0, 0, &mx, &my, &but, &ret, &keycode, &ret);
-
- wind_update (TRUE);
- wind_get (wi_handle, WF_TOP, &top_window, &ret, &ret, &ret);
-
- if (event & MU_MESAG)
- switch (msgbuff[0]) {
-
- case WM_REDRAW:
- if (msgbuff[3] == wi_handle)
- do_redraw (msgbuff[4],msgbuff[5],msgbuff[6],msgbuff[7]);
- break;
-
- case WM_NEWTOP:
- case WM_TOPPED:
- if (msgbuff[3] == wi_handle)
- wind_set(wi_handle,WF_TOP,0,0,0,0);
- break;
-
- case AC_CLOSE:
- if((msgbuff[3] == menu_id)&&(wi_handle != NO_WINDOW)){
- v_clsvwk(handle);
- wi_handle = NO_WINDOW;
- }
- break;
-
- case WM_CLOSED:
- if(msgbuff[3] == wi_handle)
- close_window ();
- break;
-
- case WM_MOVED:
- if (msgbuff[3] == wi_handle) {
- if (msgbuff[6] < MIN_WIDTH) msgbuff[6] = MIN_WIDTH;
- if (msgbuff[7] < MIN_HEIGHT) msgbuff[7] = MIN_HEIGHT;
- wind_set (wi_handle,WF_CURRXYWH,msgbuff[4],msgbuff[5],msgbuff[6],msgbuff[7]);
- wind_get (wi_handle, WF_WORKXYWH, &xwork, &ywork,&wwork,&hwork);
- do_redraw (xwork, ywork, wwork, hwork);
- }
- break;
-
- case AC_OPEN:
- if (msgbuff[4] == menu_id){
- if(wi_handle == NO_WINDOW){
- open_vwork();
- open_window();
- }
- else /* if already opened, for user convenience */
- wind_set(wi_handle,WF_TOP,0,0,0,0);
- }
- break;
-
- /*
- case WM_FULLED:
- if (fulled) {
- wind_calc (WC_WORK,WI_KIND,xold,yold,wold,hold,
- &xwork,&ywork,&wwork,&hwork);
- wind_set (wi_handle,WF_CURRXYWH,xold,yold,wold,hold);}
- else{
- wind_calc (WC_BORDER,WI_KIND,xwork,ywork,wwork,hwork,
- &xold,&yold,&wold,&hold);
- wind_calc (WC_WORK,WI_KIND,xdesk,ydesk,wdesk,hdesk,
- &xwork,&ywork,&wwork,&hwork);
- wind_set (wi_handle,WF_CURRXYWH,xdesk,ydesk,wdesk,hdesk);
- }
- fulled ^= TRUE;
- break;
- */
-
- } /* switch (msgbuff[0]) */
-
- if ((event & MU_BUTTON) && (wi_handle == top_window))
- if (!but)
- {
- if (mx >= xwork && mx <= xwork + wwork)
- {
- if (my >= ywork && my <= ywork + hwork)
- {
- scol[page] = ccol;
- sline[page] = cline;
- if (my < ywork + (hwork/2))
- {
- if (--page < 0)
- page = PAGES - 1;
- }
- else if (++page >= PAGES)
- page = 0;
- ccol = scol[page];
- cline = sline[page];
- do_redraw (xwork, ywork, wwork, hwork);
- }
- }
- }
-
- if (event & MU_KEYBD) {
- key_strike (keycode);
- }
-
- wind_update (FALSE);
-
- } /* while (TRUE) */
-
- }
-
- /****************************************************************/
- /* Draw Filled Ellipse. */
- /****************************************************************/
-
- draw_sample ()
- {
- register int l;
- /* int temp[4]; */
-
- set_window_name ();
- /* vsf_interior (handle, 2);
- vsf_style (handle, 8);
- vsf_color (handle, 0);
- *temp = xwork;
- temp[1] = ywork;
- temp[2] = xwork + wwork;
- temp[3] = ywork + hwork;
- v_bar (handle, temp);
- vsf_color (handle, 1);
- */
- for (l = 0; l < LINES; l++)
- draw_line (l);
- show_cursor ();
- }
-
- draw_line (line)
- register int line;
- {
- register char *p, *d;
- register int i, c;
-
- p = pad_ram + (page*PAGE_SIZE)+(COLUMNS*line);
- d = p + COLUMNS;
- i = *d;
- *d = '\0';
- c = ywork + (cell_height * (line+1));
- v_gtext (handle, xwork, c, p);
- *d = i;
- }
-
- key_strike (k)
- int k;
- {
- register char *p, *d;
- register int c, i;
-
- p = pad_ram + (page * PAGE_SIZE) + (cline * COLUMNS) + ccol;
- c = k & 0xff;
- erase_cursor (*p);
- if (isprint (c))
- {
- *p = c;
- erase_cursor (c);
- ccol++;
- } else if (c == '\r')
- {
- cline++;
- ccol = 0;
- } else if (c == '\t')
- ccol += 4;
- else if (c == '\b')
- {
- erase_cursor (*p--);
- *p = ' ';
- ccol--;
- } else if (c == 0x7f)
- {
- for (c=1; c < COLUMNS-ccol; c++)
- {
- *p = p[1];
- p++;
- }
- *p = ' ';
- draw_line (cline);
- } else if (c == 0)
- {
- c = (k >> 8) & 0xff;
- switch (c) {
- case 60: /* F2 Delete Line */
- p = pad_ram+(page*PAGE_SIZE)+(cline*COLUMNS);
- d = p + COLUMNS;
- for (c=cline+1; c<LINES; c++)
- for (i=0; i<COLUMNS; i++)
- *p++ = *d++;
- for (i=0; i<COLUMNS; i++)
- *p++ = ' ';
- draw_sample ();
- break;
- case 68: /* F10 Insert Line */
- p = pad_ram+((page+1)*PAGE_SIZE)-1;
- d = p-COLUMNS;
- for (c=cline+1; c<LINES; c++)
- for (i=0; i<COLUMNS; i++)
- *p-- = *d--;
- d++;
- for (i=0; i<COLUMNS; i++)
- *d++ = ' ';
- draw_sample ();
- break;
- case 0x48:
- cline--;
- break;
- case 0x4b:
- ccol--;
- break;
- case 0x50:
- cline++;
- break;
- case 0x4d:
- ccol++;
- break;
- case 82: /* Insert key */
- p =pad_ram+(page*PAGE_SIZE)+((cline+1)*COLUMNS);
- p--;
- for (c=1; c < COLUMNS-ccol; c++)
- {
- p--;
- p[1] = *p;
- }
- *p = ' ';
- draw_line (cline);
- break;
- case 59: /* F1 clear page */
- p = pad_ram + (page * PAGE_SIZE);
- for (c=0; c<PAGE_SIZE; c++)
- *p++ = ' ';
- ccol = cline = 0;
- draw_sample ();
- break;
- case 97: /* UNDO key */
- save_pad ();
- close_window ();
- break;
- }
- }
- if (ccol < 0)
- {
- ccol = COLUMNS -1;
- cline--;
- }
- else if (ccol >= COLUMNS)
- {
- ccol = 0;
- cline++;
- }
- if (cline < 0)
- cline++;
- if (cline >= LINES)
- cline--;
- show_cursor ();
- }
-
- show_cursor ()
- {
- register char *p;
-
- p = pad;
- *p = '_';
- p[1] = '\0';
- v_gtext (handle, xwork + (cell_width * ccol),
- ywork + (cell_height * (1 + cline)), p);
- }
-
- erase_cursor (c)
- int c;
- {
- register char *p;
-
- p = pad;
- *p = c;
- p[1] = '\0';
- v_gtext (handle, xwork + (cell_width * ccol),
- ywork + (cell_height * (cline + 1)), p);
- }
-
- close_window ()
- {
- wind_close (wi_handle);
- graf_shrinkbox (xwork+wwork/2,ywork+hwork/2,gl_wbox,gl_hbox,xwork,ywork,wwork,hwork);
- wind_delete (wi_handle);
- v_clsvwk (handle);
- wi_handle = NO_WINDOW;
- }
-
- set_window_name ()
- {
- strcpy (pad, " The Pad ");
- pad[9] = page + 0x31;
- wind_set (wi_handle, WF_NAME, pad, 0, 0);
- }
-
- save_pad ()
- {
- register char *p;
- register int fd;
- register long l;
-
- fd = Fopen (file, 1);
- if (fd >= 0)
- {
- l = (long)(PAGE_SIZE * PAGES);
- Fwrite (fd, l, pad_ram);
- Fclose (fd);
- }
- }
-